﻿@page "/list"
@using ShopManagment.Models
@using ShopManagment.Service.IService
@rendermode InteractiveServer
@inject IProductService ProductService
@inject IMessageService _MsgService

<PageTitle>List</PageTitle>

<h1>List</h1>

<table class="table">
    <thead>
        <tr>
            <th>商品ID</th>
            <th>商品名称</th>
            <th>商品描述</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var product in products)
        {
            <tr>
                <td>@product.ProductId</td>
                <td>@product.ProductName</td>
                <td>@product.ProductDescription</td>
                <td>
                    <a href="/add/@product.ProductId"> 编辑 </a>
                    <Popconfirm Title="Are you sure you want to delete this task?"
                                OnConfirm="() => Confirm(product.ProductId)"
                                OnCancel="Cancel">
                        <Button Type="@ButtonType.Primary">Delete</Button>
                    </Popconfirm>


                </td>
            </tr>
        }
    </tbody>
</table>

@code {
    private List<Product> products;


    protected override void OnInitialized()
    {
        products = ProductService.GetAll().ToList();
    }


    private void DeleteItem(long id)
    {
        ProductService.Delete(id);
        
    }


    private void Confirm(long id)
    {
        DeleteItem(id);
        _MsgService.Success("Clicked on Yes");
    }

    private void Cancel()
    {
        _MsgService.Error("Clicked on No");
    }
}